package com.amazon.service;

import com.alibaba.fastjson.JSON;
import com.amazon.bo.Orders;
import com.amazon.bo.Shop;
import com.amazon.dao.OrdersDao;
import com.amazon.dao.ShopDao;
import com.amazon.web.controller.vo.Order;
import com.amazon.web.util.Send253SMS;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("ordersService")
public class OrdersService {
    @Resource
    private OrdersDao ordersDao;


    //店铺列表
    public Orders addOrders(Orders order){

        Orders orders = ordersDao.getOrderByAmazonId(order.getAmazonOrderId());
        if(orders==null){
            order.setId((long)ordersDao.add(order));
        }else{
            orders.setOrderBody(order.getOrderBody());
            orders.setPurchaseDate(order.getPurchaseDate());
            ordersDao.update(orders);
        }
        return order;
    }


    public int getOrdersCount(String conn) {

        return ordersDao.getOrdersCount(conn);
    }

    public List<Map<String,Object>> getAdminOrders(int start, int length, String conn) {

        ArrayList list =(ArrayList)ordersDao.getAdminOrders(start,length,conn);
        List<Map<String,Object>> orderList=new ArrayList<>();
        for(Object item : list){

            Orders o = (Orders) item;

            Map<String,Object> order =(Map<String,Object>)JSON.parse(o.getOrderBody());

            order.put("OrderTotal","");

            order.put("PaymentMethodDetails","");

            order.put("ShippingAddress","");
            orderList.add(order);
        }
        return orderList;
    }

    //供应链产品列表
    public List<Map<String,Object>> getProductList(String conn,final int page, final int pageSize){

        List<Orders> list=ordersDao.getProductList(conn,page,pageSize);
        List<Map<String,Object>> orderList=new ArrayList<>();
        for(Object item : list){

            Orders o = (Orders) item;

            Map<String,Object> order =(Map<String,Object>)JSON.parse(o.getOrderBody());

            order.put("OrderTotal","");

            order.put("PaymentMethodDetails","");

            order.put("ShippingAddress","");
            orderList.add(order);
        }
        return orderList;
    }

    public int getOrderCount(String conn) {
        return ordersDao.getTotalCount(Orders.class,conn);
    }
}
